package com.example.agricultural_machinery_navigation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Outline;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.bumptech.glide.Glide;
import com.example.agricultural_machinery_navigation.adpter.MyRecycleViewAapter;
import com.example.agricultural_machinery_navigation.adpter.RootRecycleViewAapter;
import com.example.agricultural_machinery_navigation.bean.BannerInfo;

import com.example.agricultural_machinery_navigation.bean.RecycleViewRootInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/*****
 * 类名：MainActivity
 * 功能：主界面activity
 * ****/
public class MainActivity extends AppCompatActivity {
    //声明变量
    private XBanner xBanner;
    private RecyclerView recyclerView;

    //模拟一个返回的json数据
    private  String json ="[{\n" +
            "\t\t\"title\": \"管理\",\n" +
            "\t\t\"function\": [{\n" +
            "\t\t\t\"image\": \"https://wx2.sinaimg.cn/mw2000/007cBmgyly1h3i1u6o1g8j301c01c3yc.jpg\",\n" +
            "\t\t\t\"text\": \"地块\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx4.sinaimg.cn/mw2000/007cBmgyly1h3i1u58bkuj301c01c3yd.jpg\",\n" +
            "\t\t\t\"text\": \"农机导航\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx2.sinaimg.cn/mw2000/007cBmgyly1h3i1u4zz0zj301c01ct8j.jpg\",\n" +
            "\t\t\t\"text\": \"AB线\"\n" +
            "\t\t}]\n" +
            "\t}, {\n" +
            "\t\t\"title\": \"工具\",\n" +
            "\t\t\"function\": [{\n" +
            "\t\t\t\"image\": \"https://wx1.sinaimg.cn/mw2000/007cBmgyly1h3i1u4ssmoj301c01cq2q.jpg\",\n" +
            "\t\t\t\"text\": \"定AB线\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx3.sinaimg.cn/mw2000/007cBmgyly1h3i1u6dtxdj301c01ct8i.jpg\",\n" +
            "\t\t\t\"text\": \"测亩\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx2.sinaimg.cn/mw2000/007cBmgyly1h3i1u6tv3yj301c01cmwz.jpg\",\n" +
            "\t\t\t\"text\": \"标记物管理\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx1.sinaimg.cn/mw2000/007cBmgyly1h3i1u720c7j301c01cglf.jpg\",\n" +
            "\t\t\t\"text\": \"圈边界\"\n" +
            "\t\t}]\n" +
            "\t}, {\n" +
            "\t\t\"title\": \"服务\",\n" +
            "\t\t\"function\": [{\n" +
            "\t\t\t\"image\": \"https://wx1.sinaimg.cn/mw2000/007cBmgyly1h3i1u7cbi4j301c01cweb.jpg\",\n" +
            "\t\t\t\"text\": \"视频教学\"\n" +
            "\t\t}, {\n" +
            "\t\t\t\"image\": \"https://wx1.sinaimg.cn/mw2000/007cBmgyly1h3i1u5gecyj301c01c744.jpg\",\n" +
            "\t\t\t\"text\": \"问题反馈\"\n" +
            "\t\t}]\n" +
            "\t}\n" +
            "\n" +
            "]";


    /****
     * 方法名：onCreate
     * 功能：控件初始化加载执行
     * @param savedInstanceState 临时保存界面数据
     * ***/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    /***********
     * 方法名： initView
     * 功能：初始化UI控件
     * *********/

    private void initView() {
        //获取轮播图控件
        xBanner = findViewById(R.id.main_activity_xbanner);
        //初始化数据
        List<BannerInfo> picDatas = new ArrayList<>();
        picDatas.add(new BannerInfo(R.drawable.product4));
        picDatas.add(new BannerInfo(R.drawable.product1));
        picDatas.add(new BannerInfo(R.drawable.product2));
        picDatas.add(new BannerInfo(R.drawable.product3));
        xBanner.setBannerData(picDatas);

        //图片设置成圆角
        xBanner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 20);
            }
        });

        xBanner.setClipToOutline(true);

        //加载广告图片
        xBanner.loadImage(new XBanner.XBannerAdapter() {

            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //使用glide加载每张图片
                Glide.with(MainActivity.this).load(picDatas.get(position).getXBannerUrl()).into((ImageView) view);
            }
        });
         //初始化recycleview
         recyclerView = findViewById(R.id.main_activity_function_list_rv);
         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
         recyclerView.setLayoutManager(linearLayoutManager);
         //json解析
         Gson gson = new Gson();
         List<RecycleViewRootInfo> datalist = gson.fromJson(json, new TypeToken<List<RecycleViewRootInfo>>(){}.getType());
         //设置适配器
         RootRecycleViewAapter rootRecycleViewAapter = new RootRecycleViewAapter(this,datalist);
         recyclerView.setAdapter(rootRecycleViewAapter);
    }


}